package com.thinkgem.jeesite.modules.lottery.mobile;

import com.thinkgem.jeesite.common.utils.Result;
import com.thinkgem.jeesite.modules.lottery.entity.Activity;
import com.thinkgem.jeesite.modules.lottery.entity.Participation;
import com.thinkgem.jeesite.modules.lottery.entity.WechatAddress;
import com.thinkgem.jeesite.modules.lottery.service.ActivityService;
import com.thinkgem.jeesite.modules.lottery.service.ParticipationService;
import com.thinkgem.jeesite.modules.lottery.service.WechatAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/lottery/wechatAddress")
public class WechatAddressMobileController {

    @Autowired
    private WechatAddressService wechatAddressService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private ParticipationService participationService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(WechatAddress wechatAddress, String acid) {
        if (wechatAddress.getOpenid().equals("") || wechatAddress.getOpenid().equals("undefined")) {
            return Result.error("请先授权，再提交地址");
        }

        String addressId = wechatAddressService.saveAddress(wechatAddress);
        Participation participation = participationService.getParticipationByOpenidActivityId(wechatAddress.getOpenid(), acid);
        participation.setAddressId(addressId);
        participationService.save(participation);
        return Result.ok("地址保存成功");
    }

}
